/*
数值界面
*/
window._MANAGERNMB = {

    _MAINLABEL:null,

    _EDITBOX:null,

    _CURRENTTYPE:null,

    _CURRENTPARAM:null,

    _TYPEBUY:1,//买入
    _TYPESELL:2,//卖出
    _TYPEGIVE:3,//给与
    _TYPEGIVEUP:4,//贿赂
    _TYPEGIVEARMY:5,//归化
    _TYPEJUANZEN:6,//捐赠
    _TYPEDEFAULTDZ:7,//设置一键锻造上限
    _TYPEBUYPEOPLE:8,

    showNmbUi:function(parentNode,param){

        //console.log('param',param);

        var uiObj = _GRM.getNodeObj('nmbLabel');
        var mainLabel = uiObj.node;
        _MANAGERNMB._CURRENTTYPE = param.type;
        _MANAGERNMB._CURRENTPARAM = param;
        _MANAGERNMB._MAINLABEL = mainLabel;
        _MANAGERNMB._EDITBOX = mainLabel.getChildByName('kuan')
                                .getChildByName('inputBox')
                                .getComponent(cc.EditBox);
        if(param&&param.tipsnmb){
            _MANAGERNMB._EDITBOX.string = param.tipsnmb;
        }else{
            _MANAGERNMB._EDITBOX.string = "";
        }

        if(uiObj.isnew){
            _MANAGERNMB.initButton(mainLabel);
        }

        parentNode.addChild(mainLabel);

        return mainLabel;

    },
    
    initButton(mainLabel){

        var btnMax = mainLabel.getChildByName('kuan').getChildByName('btns').getChildByName('max');
        var btnConfirm = mainLabel.getChildByName('kuan').getChildByName('btns').getChildByName('confirm');

        mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, mainLabel);

        btnMax.on(cc.Node.EventType.TOUCH_START,function(){
            //console.log('max');
            _MANAGERNMB._EDITBOX.string = _MANAGERNMB.getMax();
        }, mainLabel);

        btnConfirm.on(cc.Node.EventType.TOUCH_START,function(){
            //console.log('btnConfirm');
            if(_MANAGERNMB.doAction()){
                _GRM.putNode(this);
            }
        }, mainLabel);

    },


    getMax:function(){
        if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEBUY){

            var param = _MANAGERNMB._CURRENTPARAM;
            var goodsInfo = param.goodsInfo;
            var buyper = _GAMEDATAHELPER.getBuyPer();
            var price = _IOF.getGoodsPrice(goodsInfo);
            var goldnmb = _GAMEDATAHELPER.getMoney();
            return Math.floor(goldnmb/(price*buyper));

        }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPESELL){
            var param = _MANAGERNMB._CURRENTPARAM;
            var goodsIndex = param.goodsIndex;
            var sourceArr = param.arr;
            var goodsInfo = sourceArr[goodsIndex];
            return goodsInfo.nmb?goodsInfo.nmb:1;
        }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEGIVE){
            var currentGold = _GAMEDATAHELPER.getMoney();
            if(currentGold>=10000000){
                return 10000000;
            }else{
                return currentGold;
            }
        }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEDEFAULTDZ){
            return _MDZ.getDzMax();
        }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEBUYPEOPLE){
            return Math.floor(_GAMEDATAHELPER.getMoney()/_JGSHOP.getPrice());
        }else{
            return _GAMEDATAHELPER.getMoney();
        }
    },

    doAction:function(){
        if(_MANAGERNMB._EDITBOX.string){
            var nmb = Math.floor(_MANAGERNMB._EDITBOX.string/1);
            if(nmb>0){
                if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPESELL){//卖出

                    var param = _MANAGERNMB._CURRENTPARAM;
                    var goodsIndex = param.goodsIndex;
                    var sourceArr = param.arr;
                    var goodsInfo = sourceArr[goodsIndex];
                    var price = _IOF.getGoodsPrice(goodsInfo);

                    if(
                    (nmb>1&&!goodsInfo.nmb)
                    ||(goodsInfo.nmb<nmb)
                    ){
                        _TIPS.showMessage('剩余数量不够,交易失败!',0);
                        return false;
                    }

                    var sellper = _GAMEDATAHELPER.getSellPer();
                    var totalgold = Math.floor(price*nmb*sellper);
                    _GAMEDATAHELPER.oprMoney(totalgold);
                    if(!goodsInfo.nmb||goodsInfo.nmb-nmb<=0){
                        sourceArr.splice(goodsIndex,1);
                    }else{
                        goodsInfo.nmb-=nmb;
                    }
                    _TIPS.showMessage('以'+(sellper*100)+'%的比例交易成功获得金钱:'+totalgold,0);
                    _MG.refreshPage(_MG._CURRENTLABEL);
                    return true;
                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEBUY){//买入

                    var param = _MANAGERNMB._CURRENTPARAM;
                    var goodsInfo = param.goodsInfo;
                    var price = _IOF.getGoodsPrice(goodsInfo);
                    var buyper = _GAMEDATAHELPER.getBuyPer();
                    var totalprice = Math.floor(price*nmb*buyper);
                    if(_GAMEDATAHELPER.oprMoney(-totalprice)){

                        if(goodsInfo.objtype=="equip"){
                            _TTCH.doCompleteTask('buyEquip',null,true);
                        }

                        for(var i=0;i<nmb;i++){
                            _GAMEDATAHELPER.pushItem(_CU.cloneObj(goodsInfo));
                        }
                        _MG.refreshPage(_MG._CURRENTLABEL);
                        _TIPS.showMessage('以'+(buyper*100)+'%的比例交易成功总共花费:'+totalprice,0);
						if(_GAMEDATAHELPER.hasTeamSkill('biancai')){
							_GAMEDATAHELPER.addLevelData('levelBright',-10);
						}
                        return true;
                    }else{
                        _TIPS.showMessage('金钱不足,交易失败!',0);
                        return false;
                    }

                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEGIVE){//给予
                    if(!_GAMEDATAHELPER.oprMoney(-nmb)){
                        _TIPS.showMessage('金钱不足,给与失败!',0);
                    }else{
                        var npc = _MANAGERNMB._CURRENTPARAM.npc;
                        if(nmb>=100000){
                            _GAMEDATAHELPER.addNpcF(npc.t,Math.floor(nmb/100000));
                            var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'003','giveSuccess');
                            _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                        }else{
                            var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'0031','giveFail');
                            _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                        }
                        return true;
                    }

                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEGIVEUP){//行贿

                    if(!_GAMEDATAHELPER.oprMoney(-nmb)){
                        _TIPS.showMessage('金钱不足,给与失败!',0);
                    }else{
                        _GAMEDATAHELPER.addLevelData('levelDark',1);
                        var npc = _MANAGERNMB._CURRENTPARAM.npc;
                        if(nmb>=100000){
                            var result = _GAMEDATAHELPER.addLevelData('levelFight',Math.floor(nmb/100000));
                            if(result){
                                var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'025','xinghuiSuccess');
                                _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                            }else{
                                var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'0261','xinghuiFail');
                                _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                            }
                        }else{
                            var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'026','xinghuiFail');
                            _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                        }
                        return true;
                    }

                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEGIVEARMY){//归化

                    if(!_GAMEDATAHELPER.oprMoney(-nmb)){
                        _TIPS.showMessage('金钱不足,给与失败!',0);
                    }else{
                        var npc = _MANAGERNMB._CURRENTPARAM.npc;
                        var npclog = _GAMEDATAHELPER.getNpcLog(npc.t);

                        if(nmb>=10000000){
                            npclog.country = window.gameData.country;
                            var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'027','guihuaSuccess');
                            _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                        }else{
                            var talkObj = _TALKHELPER.createCommonTalk(_MANAGERROLELABEL._NPC,'028','guihuaFail');
                            _MANAGERTALK.showTalk(_MANAGERROLELABEL._PARENTNODE,talkObj,_MAP);
                        }
                        return true;
                    }

                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEJUANZEN){//捐赠

                    if(!_GAMEDATAHELPER.oprMoney(-nmb)){
                        _TIPS.showMessage('金钱不足,捐赠失败!',0);
                    }else{
                        
                        var cutnmb = Math.floor(nmb/100000000);

                        if(window.gameData.levelDark<=cutnmb){
                            window.gameData.levelDark=0;
                        }else{
                            window.gameData.levelDark-=cutnmb;
                        }
                        _MAP.refreshRoleInfo();
                        _TIPS.showMessage('捐赠成功,恶名降低:'+cutnmb,0);
                        return true;
                    }

                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEDEFAULTDZ){

                    if(nmb>_MDZ.getDzMax()){
                        _TIPS.showMessage('不可超过商店锻造上限',0);
                    }else{
                        _MDZ._DEFAULTDZLV = nmb;
                        _MDZ.refreshEquipInfo();
                        return true;
                    }
                    
                }else if(_MANAGERNMB._CURRENTTYPE==_MANAGERNMB._TYPEBUYPEOPLE){
                    if(!_GAMEDATAHELPER.reduceSomeItemByName('招募券',nmb)){
                        _TIPS.showMessage('招募券不足,招募失败!',0);
                    }else{
                        var param = _MANAGERNMB._CURRENTPARAM;
                        var goodsInfo = param.goodsInfo;
                        for(var i=0;i<nmb;i++){
                            var tmpnpc = _CROF.getRandomMainRole(false,goodsInfo,null);
                            window.gameData.playerTeamBack.unshift(tmpnpc);
                        }
                        _TIPS.showMessage('购买成功,消耗招募券:'+nmb);
                    }

                }
            }
        }else{
            _TIPS.showMessage('数值不能为空！',0);
        }
        return false;
    }

}